package com.fly.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.fly.common.PaginationSupport;
import com.fly.dao.TMeasureDAO;
import com.fly.model.TMeasure;
import com.fly.model.TMeasureExample;
import com.ibatis.sqlmap.client.SqlMapExecutor;

@Repository
@SuppressWarnings({"unchecked", "deprecation", "rawtypes"})
public class TMeasureDAOImpl implements TMeasureDAO
{
    @Autowired
    private SqlMapClientTemplate sqlMapClientTemplate;
    
    @Override
    public int countByExample(TMeasureExample example)
    {
        Integer count = (Integer)sqlMapClientTemplate.queryForObject("t_measure.countByExample", example);
        return count;
    }
    
    @Override
    public int deleteAll()
    {
        int rows = sqlMapClientTemplate.delete("t_measure.deleteByExample", new TMeasureExample());
        return rows;
    }
    
    @Override
    public int deleteByExample(TMeasureExample example)
    {
        int rows = sqlMapClientTemplate.delete("t_measure.deleteByExample", example);
        return rows;
    }
    
    @Override
    public int deleteByPrimaryKey(Long id)
    {
        TMeasure key = new TMeasure();
        key.setId(id);
        int rows = sqlMapClientTemplate.delete("t_measure.deleteByPrimaryKey", key);
        return rows;
    }
    
    @Override
    public void deleteByPrimaryKey(final List<Long> ids)
    {
        sqlMapClientTemplate.execute(new SqlMapClientCallback()
        {
            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor)
                throws SQLException
            {
                executor.startBatch();
                for (Long id : ids)
                {
                    TMeasure it = new TMeasure();
                    it.setId(id);
                    executor.delete("t_measure.deleteByPrimaryKey", it);
                }
                executor.executeBatch();
                return null;
            }
        });
    }
    
    @Override
    public void insert(TMeasure record)
    {
        sqlMapClientTemplate.insert("t_measure.insert", record);
    }
    
    @Override
    public void insert(final List<TMeasure> records)
    {
        sqlMapClientTemplate.execute(new SqlMapClientCallback()
        {
            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor)
                throws SQLException
            {
                executor.startBatch();
                for (TMeasure it : records)
                {
                    executor.insert("t_measure.insert", it);
                }
                executor.executeBatch();
                return null;
            }
        });
    }
    
    @Override
    public void insertSelective(TMeasure record)
    {
        sqlMapClientTemplate.insert("t_measure.insertSelective", record);
    }
    
    @Override
    public void insertSelective(final List<TMeasure> records)
    {
        sqlMapClientTemplate.execute(new SqlMapClientCallback()
        {
            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor)
                throws SQLException
            {
                executor.startBatch();
                for (TMeasure it : records)
                {
                    executor.insert("t_measure.insertSelective", it);
                }
                executor.executeBatch();
                return null;
            }
        });
    }
    
    @Override
    public List<TMeasure> selectAll()
    {
        List<TMeasure> list = sqlMapClientTemplate.queryForList("t_measure.selectByExample", null);
        return list;
    }
    
    @Override
    public List<TMeasure> selectByExample(TMeasureExample example)
    {
        List<TMeasure> list = sqlMapClientTemplate.queryForList("t_measure.selectByExample", example);
        return list;
    }
    
    @Override
    public TMeasure selectByPrimaryKey(Long id)
    {
        TMeasure key = new TMeasure();
        key.setId(id);
        TMeasure record = (TMeasure)sqlMapClientTemplate.queryForObject("t_measure.selectByPrimaryKey", key);
        return record;
    }
    
    @Override
    public int updateByExampleSelective(TMeasure record, TMeasureExample example)
    {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = sqlMapClientTemplate.update("t_measure.updateByExampleSelective", parms);
        return rows;
    }
    
    @Override
    public int updateByExample(TMeasure record, TMeasureExample example)
    {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = sqlMapClientTemplate.update("t_measure.updateByExample", parms);
        return rows;
    }
    
    @Override
    public int updateByPrimaryKeySelective(TMeasure record)
    {
        int rows = sqlMapClientTemplate.update("t_measure.updateByPrimaryKeySelective", record);
        return rows;
    }
    
    @Override
    public void updateByPrimaryKeySelective(final List<TMeasure> records)
    {
        sqlMapClientTemplate.execute(new SqlMapClientCallback()
        {
            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor)
                throws SQLException
            {
                executor.startBatch();
                for (TMeasure it : records)
                {
                    executor.update("t_measure.updateByPrimaryKeySelective", it);
                }
                executor.executeBatch();
                return null;
            }
        });
    }
    
    @Override
    public int updateByPrimaryKey(TMeasure record)
    {
        int rows = sqlMapClientTemplate.update("t_measure.updateByPrimaryKey", record);
        return rows;
    }
    
    @Override
    public void updateByPrimaryKey(final List<TMeasure> records)
    {
        sqlMapClientTemplate.execute(new SqlMapClientCallback()
        {
            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor)
                throws SQLException
            {
                executor.startBatch();
                for (TMeasure it : records)
                {
                    executor.update("t_measure.updateByPrimaryKey", it);
                }
                executor.executeBatch();
                return null;
            }
        });
    }
    
    @Override
    public PaginationSupport queryPagingEntities(TMeasureExample example, int pageNo, int pageSize)
    {
        pageNo = Math.max(1, pageNo);
        pageSize = Math.max(2, pageSize);
        int totalcount = countByExample(example);
        PaginationSupport entites = new PaginationSupport(totalcount, pageNo, pageSize);
        String statementName = "t_measure.selectByExample";
        List<TMeasure> entityList = sqlMapClientTemplate.queryForList(statementName, example, pageSize * (pageNo - 1), pageSize);
        entites.setItems(entityList);
        return entites;
    }
    
    private static class UpdateByExampleParms extends TMeasureExample
    {
        private Object record;
        
        public UpdateByExampleParms(Object record, TMeasureExample example)
        {
            super(example);
            this.record = record;
        }
        
        @SuppressWarnings("unused")
        public Object getRecord()
        {
            return record;
        }
    }
}